From 4246051d6c8738f26f27a9c6663d5d22613f65ca Mon Sep 17 00:00:00 2001 From: ehsan6sha Date: Wed, 2 Aug 2023 00:12:47 -0400 Subject: [PATCH 1/3] added some debugging --- src/private_forest.rs | 98 ++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/src/private_forest.rs b/src/private_forest.rs index f9eaf48..436032a 100644 --- a/src/private_forest.rs +++ b/src/private_forest.rs @@ -99,10 +99,11 @@ impl<'a> PrivateDirectoryHelper<'a> { async fn setup_seeded_keypair_access( forest: &mut Rc, access_key: AccessKey, - store: &impl BlockStore, + store: &mut FFIFriendlyBlockStore<'a>, seed: [u8; 32], ) -> Result<[u8; 32]> { let root_did = Self::bytes_to_hex_str(&seed); + println!("wnfsutils: setup_seeded_keypair_access with seed: {:?} and root_did: {:?} and access_key: {:?}", seed, root_did, access_key); let exchange_keypair = SeededExchangeKey::from_seed(seed.clone())?; // Store the public key inside some public WNFS. @@ -144,7 +145,6 @@ impl<'a> PrivateDirectoryHelper<'a> { store, ) .await?; - Ok(seed) } @@ -167,7 +167,9 @@ impl<'a> PrivateDirectoryHelper<'a> { let forest_res = PrivateDirectoryHelper::create_private_forest(store.to_owned()).await; if forest_res.is_ok() { - let (forest, _) = &mut forest_res.unwrap(); + let (forest, _) = &mut forest_res + .ok() + .unwrap(); // Create a root directory from the ratchet_seed, inumber and namefilter. Directory gets saved in forest. let root_dir_res = PrivateDirectory::new_with_seed_and_store( Namefilter::default(), @@ -182,20 +184,12 @@ impl<'a> PrivateDirectoryHelper<'a> { if root_dir_res.is_ok() { // Private ref contains data and keys for fetching and decrypting the directory node in the private forest. - let root_dir = &mut root_dir_res.unwrap(); + let root_dir = &mut root_dir_res.ok().unwrap(); let access_key = root_dir.as_node().store(forest, store, rng).await; if access_key.is_ok() { - let forest_cid = PrivateDirectoryHelper::update_private_forest( - store.to_owned(), - forest.to_owned(), - ) - .await; - if forest_cid.is_ok() { - unsafe { - STATE.lock().unwrap().update(true, wnfs_key.to_owned()); - } - let seed: [u8; 32] = wnfs_key.try_into().expect("Length mismatch"); + let seed: [u8; 32] = wnfs_key.to_owned().try_into().expect("Length mismatch"); let access_key_unwrapped = access_key.ok().unwrap(); + println!("wnfsutils: init accessKey is {:?} and seed is: {:?}",access_key_unwrapped, seed); let seed_res = Self::setup_seeded_keypair_access( forest, access_key_unwrapped.to_owned(), @@ -203,7 +197,15 @@ impl<'a> PrivateDirectoryHelper<'a> { seed, ) .await; - if seed_res.is_ok() { + let forest_cid = PrivateDirectoryHelper::update_private_forest( + store.to_owned(), + forest.to_owned(), + ) + .await; + if forest_cid.is_ok() { + unsafe { + STATE.lock().unwrap().update(true, wnfs_key.to_owned()); + } Ok(( Self { store: store.to_owned(), @@ -221,13 +223,6 @@ impl<'a> PrivateDirectoryHelper<'a> { ); Err(seed_res.err().unwrap().to_string()) } - } else { - trace!( - "wnfsError in init: {:?}", - forest_cid.as_ref().err().unwrap().to_string() - ); - Err(forest_cid.err().unwrap().to_string()) - } } else { trace!( "wnfsError in init: {:?}", @@ -254,6 +249,7 @@ impl<'a> PrivateDirectoryHelper<'a> { forest_cid: Cid, wnfs_key: Vec, ) -> Result, String> { + trace!("wnfsutils: load_with_wnfs_key started"); let rng = &mut thread_rng(); let root_did: String; let seed: [u8; 32]; @@ -266,13 +262,15 @@ impl<'a> PrivateDirectoryHelper<'a> { root_did = Self::bytes_to_hex_str(&wnfs_key); seed = wnfs_key.to_owned().try_into().expect("Length mismatch"); } + println!("wnfsutils: load_with_wnfs_key with seed: {:?} and root_did: {:?} ", seed, root_did); let exchange_keypair_res = SeededExchangeKey::from_seed(seed); if exchange_keypair_res.is_ok() { let exchange_keypair = exchange_keypair_res.ok().unwrap(); + println!("wnfsutils: load_with_wnfs_key with forest_cid: {:?}", forest_cid); let forest_res = PrivateDirectoryHelper::load_private_forest(store.to_owned(), forest_cid).await; if forest_res.is_ok() { - let forest = &mut forest_res.unwrap(); + let forest = &mut forest_res.ok().unwrap(); // Create a root directory from the ratchet_seed, inumber and namefilter. Directory gets saved in forest. // Re-load private node from forest let counter_res = recipient::find_latest_share_counter( @@ -286,19 +284,29 @@ impl<'a> PrivateDirectoryHelper<'a> { .await; if counter_res.is_ok() { let counter = counter_res.ok().unwrap().map(|x| x + 1).unwrap_or_default(); + println!("wnfsutils: load_with_wnfs_key with counter: {:?}", counter); let label = sharer::create_share_label( counter, &root_did, &exchange_keypair.encode_public_key(), ); let node_res = - recipient::receive_share(label, &exchange_keypair, forest, store).await; + recipient::receive_share( + label, + &exchange_keypair, + forest, + store + ).await; if node_res.is_ok() { let node = node_res.ok().unwrap(); let latest_node = node.search_latest(forest, store).await; if latest_node.is_ok() { - let latest_root_dir = latest_node.unwrap().as_dir(); + let latest_root_dir = + latest_node + .ok() + .unwrap() + .as_dir(); if latest_root_dir.is_ok() { unsafe { STATE.lock().unwrap().update(true, wnfs_key.to_owned()); @@ -306,18 +314,18 @@ impl<'a> PrivateDirectoryHelper<'a> { Ok(Self { store: store.to_owned(), forest: forest.to_owned(), - root_dir: latest_root_dir.unwrap(), + root_dir: latest_root_dir.ok().unwrap(), rng: rng.to_owned(), }) } else { - trace!( + println!( "wnfsError in load_with_wnfs_key: {:?}", latest_root_dir.as_ref().err().unwrap().to_string() ); Err(latest_root_dir.err().unwrap().to_string()) } } else { - trace!( + println!( "wnfsError occured in load_with_wnfs_key: {:?}", latest_node.as_ref().to_owned().err().unwrap().to_string() ); @@ -325,7 +333,7 @@ impl<'a> PrivateDirectoryHelper<'a> { } } else { let err = node_res.as_ref().to_owned().err().unwrap().to_string(); - trace!( + println!( "wnfsError occured in load_with_wnfs_key node_res: {:?}", err ); @@ -333,7 +341,7 @@ impl<'a> PrivateDirectoryHelper<'a> { } } else { let err = counter_res.as_ref().to_owned().err().unwrap().to_string(); - trace!( + println!( "wnfsError occured in load_with_wnfs_key counter_res: {:?}", err ); @@ -341,7 +349,7 @@ impl<'a> PrivateDirectoryHelper<'a> { } } else { let err = forest_res.as_ref().to_owned().err().unwrap().to_string(); - trace!("wnfsError occured in load_with_wnfs_key: {:?}", err); + println!("wnfsError occured in load_with_wnfs_key: {:?}", err); Err(err) } } else { @@ -351,7 +359,7 @@ impl<'a> PrivateDirectoryHelper<'a> { .err() .unwrap() .to_string(); - trace!( + println!( "wnfsError occured in load_with_wnfs_key exchange_keypair_res: {:?}", err ); @@ -385,7 +393,7 @@ impl<'a> PrivateDirectoryHelper<'a> { // Deserialize private forest from the blockstore. let forest = store.get_deserializable::(&forest_cid).await; if forest.is_ok() { - Ok(Rc::new(forest.unwrap())) + Ok(Rc::new(forest.ok().unwrap())) } else { trace!( "wnfsError occured in load__private_forest: {:?}", @@ -1276,6 +1284,30 @@ mod private_tests { .read_file(&["root".into(), "hello".into(), "world.txt".into()]) .await; assert_eq!(content.ok(), None); + println!("**************************reload test*****************"); + let helper_reloaded = + &mut PrivateDirectoryHelper::reload( + blockstore, + cid + ) + .await + .unwrap(); + let cid_reloaded = helper_reloaded + .write_file( + &["root".into(), "hello2".into(), "world.txt".into()], + b"hello, world2!".to_vec(), + 0, + ) + .await + .unwrap(); + + let ls_result_reloaded = helper_reloaded.ls_files(&["root".into()]).await.unwrap(); + println!("ls_result_reloaded: {:?}", ls_result_reloaded); + assert_eq!(ls_result_reloaded.get(0).unwrap().0, "hello"); + assert_eq!(ls_result_reloaded.get(1).unwrap().0, "hi"); + assert_eq!(ls_result_reloaded.get(2).unwrap().0, "hello2"); + println!("cid_reloaded: {:?}", cid_reloaded); + // let last_root_dir = helper // .get_root_dir(forest.to_owned(), access_key.to_owned()) // .await From 654e19e2865316e2ff8c90b0123c19f64b95bb50 Mon Sep 17 00:00:00 2001 From: ehsan6sha Date: Wed, 2 Aug 2023 11:35:12 -0400 Subject: [PATCH 2/3] corrected the bug in reload --- Cargo.toml | 2 +- src/private_forest.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e355fe9..b6e473a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wnfsutils" -version = "1.1.1" +version = "1.1.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/private_forest.rs b/src/private_forest.rs index 436032a..143b3b9 100644 --- a/src/private_forest.rs +++ b/src/private_forest.rs @@ -283,7 +283,7 @@ impl<'a> PrivateDirectoryHelper<'a> { ) .await; if counter_res.is_ok() { - let counter = counter_res.ok().unwrap().map(|x| x + 1).unwrap_or_default(); + let counter = counter_res.ok().unwrap().map(|x| x).unwrap_or_default(); println!("wnfsutils: load_with_wnfs_key with counter: {:?}", counter); let label = sharer::create_share_label( counter, @@ -1304,8 +1304,8 @@ mod private_tests { let ls_result_reloaded = helper_reloaded.ls_files(&["root".into()]).await.unwrap(); println!("ls_result_reloaded: {:?}", ls_result_reloaded); assert_eq!(ls_result_reloaded.get(0).unwrap().0, "hello"); - assert_eq!(ls_result_reloaded.get(1).unwrap().0, "hi"); - assert_eq!(ls_result_reloaded.get(2).unwrap().0, "hello2"); + assert_eq!(ls_result_reloaded.get(2).unwrap().0, "hi"); + assert_eq!(ls_result_reloaded.get(1).unwrap().0, "hello2"); println!("cid_reloaded: {:?}", cid_reloaded); // let last_root_dir = helper From 06e863c159b38dd72e2bcc64c0e326ecdbfc0aec Mon Sep 17 00:00:00 2001 From: ehsan6sha Date: Wed, 2 Aug 2023 11:37:00 -0400 Subject: [PATCH 3/3] cargo fmt --all --- src/private_forest.rs | 108 ++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 57 deletions(-) diff --git a/src/private_forest.rs b/src/private_forest.rs index 143b3b9..d8f419b 100644 --- a/src/private_forest.rs +++ b/src/private_forest.rs @@ -167,9 +167,7 @@ impl<'a> PrivateDirectoryHelper<'a> { let forest_res = PrivateDirectoryHelper::create_private_forest(store.to_owned()).await; if forest_res.is_ok() { - let (forest, _) = &mut forest_res - .ok() - .unwrap(); + let (forest, _) = &mut forest_res.ok().unwrap(); // Create a root directory from the ratchet_seed, inumber and namefilter. Directory gets saved in forest. let root_dir_res = PrivateDirectory::new_with_seed_and_store( Namefilter::default(), @@ -187,42 +185,45 @@ impl<'a> PrivateDirectoryHelper<'a> { let root_dir = &mut root_dir_res.ok().unwrap(); let access_key = root_dir.as_node().store(forest, store, rng).await; if access_key.is_ok() { - let seed: [u8; 32] = wnfs_key.to_owned().try_into().expect("Length mismatch"); - let access_key_unwrapped = access_key.ok().unwrap(); - println!("wnfsutils: init accessKey is {:?} and seed is: {:?}",access_key_unwrapped, seed); - let seed_res = Self::setup_seeded_keypair_access( - forest, - access_key_unwrapped.to_owned(), - store, - seed, - ) - .await; - let forest_cid = PrivateDirectoryHelper::update_private_forest( - store.to_owned(), - forest.to_owned(), - ) - .await; - if forest_cid.is_ok() { - unsafe { - STATE.lock().unwrap().update(true, wnfs_key.to_owned()); - } - Ok(( - Self { - store: store.to_owned(), - forest: forest.to_owned(), - root_dir: root_dir.to_owned(), - rng: rng.to_owned(), - }, - access_key_unwrapped, - forest_cid.unwrap(), - )) - } else { - trace!( - "wnfsError in init:setup_seeded_keypair_access : {:?}", - seed_res.as_ref().err().unwrap().to_string() - ); - Err(seed_res.err().unwrap().to_string()) + let seed: [u8; 32] = wnfs_key.to_owned().try_into().expect("Length mismatch"); + let access_key_unwrapped = access_key.ok().unwrap(); + println!( + "wnfsutils: init accessKey is {:?} and seed is: {:?}", + access_key_unwrapped, seed + ); + let seed_res = Self::setup_seeded_keypair_access( + forest, + access_key_unwrapped.to_owned(), + store, + seed, + ) + .await; + let forest_cid = PrivateDirectoryHelper::update_private_forest( + store.to_owned(), + forest.to_owned(), + ) + .await; + if forest_cid.is_ok() { + unsafe { + STATE.lock().unwrap().update(true, wnfs_key.to_owned()); } + Ok(( + Self { + store: store.to_owned(), + forest: forest.to_owned(), + root_dir: root_dir.to_owned(), + rng: rng.to_owned(), + }, + access_key_unwrapped, + forest_cid.unwrap(), + )) + } else { + trace!( + "wnfsError in init:setup_seeded_keypair_access : {:?}", + seed_res.as_ref().err().unwrap().to_string() + ); + Err(seed_res.err().unwrap().to_string()) + } } else { trace!( "wnfsError in init: {:?}", @@ -262,11 +263,17 @@ impl<'a> PrivateDirectoryHelper<'a> { root_did = Self::bytes_to_hex_str(&wnfs_key); seed = wnfs_key.to_owned().try_into().expect("Length mismatch"); } - println!("wnfsutils: load_with_wnfs_key with seed: {:?} and root_did: {:?} ", seed, root_did); + println!( + "wnfsutils: load_with_wnfs_key with seed: {:?} and root_did: {:?} ", + seed, root_did + ); let exchange_keypair_res = SeededExchangeKey::from_seed(seed); if exchange_keypair_res.is_ok() { let exchange_keypair = exchange_keypair_res.ok().unwrap(); - println!("wnfsutils: load_with_wnfs_key with forest_cid: {:?}", forest_cid); + println!( + "wnfsutils: load_with_wnfs_key with forest_cid: {:?}", + forest_cid + ); let forest_res = PrivateDirectoryHelper::load_private_forest(store.to_owned(), forest_cid).await; if forest_res.is_ok() { @@ -291,22 +298,13 @@ impl<'a> PrivateDirectoryHelper<'a> { &exchange_keypair.encode_public_key(), ); let node_res = - recipient::receive_share( - label, - &exchange_keypair, - forest, - store - ).await; + recipient::receive_share(label, &exchange_keypair, forest, store).await; if node_res.is_ok() { let node = node_res.ok().unwrap(); let latest_node = node.search_latest(forest, store).await; if latest_node.is_ok() { - let latest_root_dir = - latest_node - .ok() - .unwrap() - .as_dir(); + let latest_root_dir = latest_node.ok().unwrap().as_dir(); if latest_root_dir.is_ok() { unsafe { STATE.lock().unwrap().update(true, wnfs_key.to_owned()); @@ -1285,11 +1283,7 @@ mod private_tests { .await; assert_eq!(content.ok(), None); println!("**************************reload test*****************"); - let helper_reloaded = - &mut PrivateDirectoryHelper::reload( - blockstore, - cid - ) + let helper_reloaded = &mut PrivateDirectoryHelper::reload(blockstore, cid) .await .unwrap(); let cid_reloaded = helper_reloaded @@ -1307,7 +1301,7 @@ mod private_tests { assert_eq!(ls_result_reloaded.get(2).unwrap().0, "hi"); assert_eq!(ls_result_reloaded.get(1).unwrap().0, "hello2"); println!("cid_reloaded: {:?}", cid_reloaded); - + // let last_root_dir = helper // .get_root_dir(forest.to_owned(), access_key.to_owned()) // .await